// index.ts
import Request from "./index";
import { useStore } from "../stores/index";
// import type { AxiosResponse } from "axios";
// import { BASE_URL, TIME_OUT } from "./config";

const request = new Request({
  baseURL: "http://127.0.0.1:4523/m1/1764398-0-default",
  timeout: 5000,
  interceptors: {
    // 实例传入请求拦截器
    // 实例拦截器是为了保证封装的灵活性，因为每一个实例中的拦截后处理的操作可能是不一样的，所以在定义实例时，允许我们传入拦截器
    requestInterceptors: (config) => {
      console.log("-- 实例传入请求拦截器.success --", config);
      const store = useStore();

      // const token = localStorage.getItem("token") as string;
      const token = store.token;

      if (token) {
        config.headers!.Authorization = token;
      }

      return config;
    },
    requestInterceptorsCatch: (err) => {
      console.log("-- 实例传入请求拦截器.error --", err);
      return err;
    },
    // 实例响应拦截器
    responseInterceptors: (result) => {
      console.log("-- 实例传入响应拦截器.success --", result);
      return result;
    },
    responseInterceptorsCatch: (err) => {
      console.log("-- 实例传入响应拦截器.error --", err);
      return err;
    },
  },
});

// 取消请求
export const cancelRequest = (url: string | string[]) => {
  return request.cancelRequest(url);
};
// 取消全部请求
export const cancelAllRequest = () => {
  return request.cancelAllRequest();
};

export default request;
